package cn.darkhorse.basic.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.SysRegion;
import com.ruoyi.system.service.SysRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.darkhorse.basic.mapper.QmsProcessorsMapper;
import cn.darkhorse.basic.domain.QmsProcessors;
import cn.darkhorse.basic.service.IQmsProcessorsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 加工企业管理Service业务层处理
 *
 * @author ytq
 * @date 2024-07-18
 */
@Service
public class QmsProcessorsServiceImpl extends ServiceImpl<QmsProcessorsMapper, QmsProcessors> implements IQmsProcessorsService
{
    @Autowired
    private QmsProcessorsMapper qmsProcessorsMapper;
    @Autowired
    private SysRegionService sysRegionService;

    /**
     * 查询加工企业管理
     *
     * @param id 加工企业管理主键
     * @return 加工企业管理
     */
    @Override
    public QmsProcessors selectQmsProcessorsById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询加工企业管理列表
     *
     * @param qmsProcessors 加工企业管理
     * @return 加工企业管理
     */
    @Override
    public List<QmsProcessors> selectQmsProcessorsList(QmsProcessors qmsProcessors)
    {
        LambdaQueryWrapper<QmsProcessors> queryWrapper = new LambdaQueryWrapper<QmsProcessors>();
        if (qmsProcessors.getEnterpriseName()!=null){
            queryWrapper.like(QmsProcessors::getEnterpriseName, qmsProcessors.getEnterpriseName());
        }
        if (qmsProcessors.getEnterpriseCode()!=null){
            queryWrapper.like(QmsProcessors::getEnterpriseCode, qmsProcessors.getEnterpriseCode());
        }
        if (qmsProcessors.getRegiment()!=null){
            queryWrapper.like(QmsProcessors::getRegiment, qmsProcessors.getRegiment());
        }
        if (qmsProcessors.getContract()!=null){
            queryWrapper.like(QmsProcessors::getContract, qmsProcessors.getContract());
        }
        queryWrapper.orderByDesc(QmsProcessors::getCreateTime);
        return list(queryWrapper);
    }

    /**
     * 新增加工企业管理
     *
     * @param qmsProcessors 加工企业管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveQmsProcessors(QmsProcessors qmsProcessors)
    {
        SysRegion region = sysRegionService.lambdaQuery()
                .in(SysRegion::getCode, qmsProcessors.getProvince(), qmsProcessors.getCity(), qmsProcessors.getCounty()).orderByDesc(SysRegion::getLevel)
                .last("limit 1").one();
        if (region != null){
            if (qmsProcessors.getDetailedAddress()!=null){
                qmsProcessors.setFullAddress(region.getFullName() + qmsProcessors.getDetailedAddress());
            }else {
                qmsProcessors.setFullAddress(region.getFullName());
            }
        }
        String enterpriseCode = qmsProcessors.getEnterpriseCode();
        Long count;
        if (qmsProcessors.getId() == null){
            count = lambdaQuery().eq(QmsProcessors::getEnterpriseCode, enterpriseCode).count();
        }else {
            count = lambdaQuery().eq(QmsProcessors::getEnterpriseCode, enterpriseCode).ne(QmsProcessors::getId, qmsProcessors.getId()).count();
        }
        if (count > 0){
            throw new RuntimeException("企业编码重复");
        }

        return saveOrUpdate(qmsProcessors);
    }


    /**
     * 批量删除加工企业管理
     *
     * @param ids 需要删除的加工企业管理主键
     * @return 结果
     */
    @Override
    public int deleteQmsProcessorsByIds(Long[] ids)
    {
        return qmsProcessorsMapper.deleteQmsProcessorsByIds(ids);
    }

    /**
     * 删除加工企业管理信息
     *
     * @param id 加工企业管理主键
     * @return 结果
     */
    @Override
    public int deleteQmsProcessorsById(Long id)
    {
        return qmsProcessorsMapper.deleteQmsProcessorsById(id);
    }
}
